<!doctype html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="LVS,keepalived," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.1" />






<meta name="description" content="keepalived 的启动脚本1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969">
<meta name="keywords" content="LVS,keepalived">
<meta property="og:type" content="article">
<meta property="og:title" content="keepalived 的启动脚本和安装脚本 的结合配置文件详解">
<meta property="og:url" content="https://hcldirgit.github.io/2017/09/03/LVS/5. keepalived 的启动脚本和安装脚本/index.html">
<meta property="og:site_name" content="失落的乐章">
<meta property="og:description" content="keepalived 的启动脚本1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2017-09-01T08:51:57.915Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="keepalived 的启动脚本和安装脚本 的结合配置文件详解">
<meta name="twitter:description" content="keepalived 的启动脚本1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://hcldirgit.github.io/2017/09/03/LVS/5. keepalived 的启动脚本和安装脚本/"/>





  <title>keepalived 的启动脚本和安装脚本 的结合配置文件详解 | 失落的乐章</title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  




<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
  ga('create', '85*****1', 'auto');
  ga('send', 'pageview');
</script>


  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?87980c**************99ec5e26fb5";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>











  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">失落的乐章</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">技术面前，永远都是学生。</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-message">
          <a href="/message" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-external-link"></i> <br />
            
            留言
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://hcldirgit.github.io/2017/09/03/LVS/5. keepalived 的启动脚本和安装脚本/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="失落的乐章">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/0.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="失落的乐章">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">keepalived 的启动脚本和安装脚本 的结合配置文件详解</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-09-03T02:04:46+08:00">
                2017-09-03
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="keepalived-的启动脚本"><a href="#keepalived-的启动脚本" class="headerlink" title="keepalived 的启动脚本"></a>keepalived 的启动脚本</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div><div class="line">144</div><div class="line">145</div><div class="line">146</div><div class="line">147</div><div class="line">148</div><div class="line">149</div><div class="line">150</div><div class="line">151</div><div class="line">152</div><div class="line">153</div><div class="line">154</div><div class="line">155</div><div class="line">156</div><div class="line">157</div><div class="line">158</div><div class="line">159</div><div class="line">160</div><div class="line">161</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/sh</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># Startup script for the Keepalived daemon</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># processname: keepalived</span></div><div class="line"><span class="comment"># pidfile: /var/run/keepalived.pid</span></div><div class="line"><span class="comment"># config: /etc/keepalived/keepalived.conf</span></div><div class="line"><span class="comment"># chkconfig: - 21 79</span></div><div class="line"><span class="comment"># description: Start and stop Keepalived</span></div><div class="line"></div><div class="line">. /etc/init.d/<span class="built_in">functions</span></div><div class="line"></div><div class="line"><span class="comment"># Source configuration file (we set KEEPALIVED_OPTIONS there)</span></div><div class="line"><span class="comment">#. /etc/sysconfig/keepalived</span></div><div class="line">KEEP_CONF=<span class="string">"/export/servers/keepalived-1.2.13/conf/keepalived.conf"</span></div><div class="line">KEEP_SBIN=<span class="string">"/export/servers/keepalived-1.2.13/sbin/keepalived"</span></div><div class="line">KEEP_PID=<span class="string">"/var/run/keepalived.pid "</span></div><div class="line"></div><div class="line">RETVAL=0</div><div class="line"></div><div class="line">prog=<span class="string">"keepalived"</span></div><div class="line"></div><div class="line"><span class="function"><span class="title">start</span></span>() &#123;</div><div class="line">    <span class="built_in">echo</span> -n $<span class="string">"Starting <span class="variable">$prog</span>: "</span></div><div class="line">    daemon <span class="variable">$KEEP_SBIN</span> --use-file=<span class="variable">$KEEP_CONF</span></div><div class="line">    RETVAL=$?</div><div class="line">    <span class="built_in">echo</span></div><div class="line">    <span class="built_in">return</span> <span class="variable">$RETVAL</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">stop</span></span>() &#123;</div><div class="line">    <span class="built_in">echo</span> -n $<span class="string">"Stopping <span class="variable">$prog</span>: "</span></div><div class="line">    killproc -p <span class="variable">$KEEP_PID</span> <span class="variable">$KEEP_SBIN</span> -TERM</div><div class="line">    RETVAL=$?</div><div class="line">    <span class="built_in">echo</span></div><div class="line">    <span class="comment">#[ $RETVAL -eq 0 ] &amp;&amp; rm -f /var/lock/subsys/$prog</span></div><div class="line">    <span class="built_in">return</span> <span class="variable">$RETVAL</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">reload</span></span>() &#123;</div><div class="line">    <span class="built_in">echo</span> -n $<span class="string">"Reloading <span class="variable">$prog</span>: "</span></div><div class="line">    <span class="comment">#killproc keepalived -1</span></div><div class="line">    killproc -p <span class="variable">$KEEP_PID</span> <span class="variable">$KEEP_SBIN</span> -HUP</div><div class="line">    RETVAL=$?</div><div class="line">    <span class="built_in">echo</span></div><div class="line">    <span class="built_in">return</span> <span class="variable">$RETVAL</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment"># See how we were called.</span></div><div class="line"><span class="keyword">case</span> <span class="string">"<span class="variable">$1</span>"</span> <span class="keyword">in</span></div><div class="line">    start)</div><div class="line">        start</div><div class="line">        ;;</div><div class="line">    stop)</div><div class="line">        stop</div><div class="line">        ;;</div><div class="line">    reload)</div><div class="line">        reload</div><div class="line">        ;;</div><div class="line">    restart)</div><div class="line">        stop</div><div class="line">        start</div><div class="line">        ;;</div><div class="line">    condrestart)</div><div class="line">        <span class="keyword">if</span> [ -f /var/lock/subsys/<span class="variable">$prog</span> ]; <span class="keyword">then</span></div><div class="line">            stop</div><div class="line">            start</div><div class="line">        <span class="keyword">fi</span></div><div class="line">        ;;</div><div class="line">    status)</div><div class="line">        status keepalived</div><div class="line">        RETVAL=$?</div><div class="line">        ;;</div><div class="line">    *)</div><div class="line">        <span class="built_in">echo</span> <span class="string">"Usage: <span class="variable">$0</span> &#123;start|stop|reload|restart|condrestart|status&#125;"</span></div><div class="line">        RETVAL=1</div><div class="line"><span class="keyword">esac</span></div><div class="line"></div><div class="line"><span class="built_in">exit</span> <span class="variable">$RETVAL</span></div><div class="line">=======</div><div class="line"><span class="meta">#!/bin/sh</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># Startup script for the Keepalived daemon</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># processname: keepalived</span></div><div class="line"><span class="comment"># pidfile: /var/run/keepalived.pid</span></div><div class="line"><span class="comment"># config: /etc/keepalived/keepalived.conf</span></div><div class="line"><span class="comment"># chkconfig: - 21 79</span></div><div class="line"><span class="comment"># description: Start and stop Keepalived</span></div><div class="line"></div><div class="line"><span class="comment"># Source function library</span></div><div class="line">. /etc/init.d/<span class="built_in">functions</span></div><div class="line"></div><div class="line"><span class="comment"># Source configuration file (we set KEEPALIVED_OPTIONS there)</span></div><div class="line"><span class="comment">#. /etc/sysconfig/keepalived</span></div><div class="line">KEEP_CONF=<span class="string">"/export/servers/keepalived-1.2.13/conf/keepalived.conf"</span></div><div class="line">KEEP_SBIN=<span class="string">"/export/servers/keepalived-1.2.13/sbin/keepalived"</span></div><div class="line">KEEP_PID=<span class="string">"/var/run/keepalived.pid "</span></div><div class="line"></div><div class="line">RETVAL=0</div><div class="line"></div><div class="line">prog=<span class="string">"keepalived"</span></div><div class="line"></div><div class="line"><span class="function"><span class="title">start</span></span>() &#123;</div><div class="line">    <span class="built_in">echo</span> -n $<span class="string">"Starting <span class="variable">$prog</span>: "</span></div><div class="line">    daemon <span class="variable">$KEEP_SBIN</span> --use-file=<span class="variable">$KEEP_CONF</span></div><div class="line">    RETVAL=$?</div><div class="line">    <span class="built_in">echo</span></div><div class="line">    <span class="built_in">return</span> <span class="variable">$RETVAL</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">stop</span></span>() &#123;</div><div class="line">    <span class="built_in">echo</span> -n $<span class="string">"Stopping <span class="variable">$prog</span>: "</span></div><div class="line">    killproc -p <span class="variable">$KEEP_PID</span> <span class="variable">$KEEP_SBIN</span> -TERM</div><div class="line">    RETVAL=$?</div><div class="line">    <span class="built_in">echo</span></div><div class="line">    <span class="comment">#[ $RETVAL -eq 0 ] &amp;&amp; rm -f /var/lock/subsys/$prog</span></div><div class="line">    <span class="built_in">return</span> <span class="variable">$RETVAL</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">reload</span></span>() &#123;</div><div class="line">    <span class="built_in">echo</span> -n $<span class="string">"Reloading <span class="variable">$prog</span>: "</span></div><div class="line">    <span class="comment">#killproc keepalived -1</span></div><div class="line">    killproc -p <span class="variable">$KEEP_PID</span> <span class="variable">$KEEP_SBIN</span> -HUP</div><div class="line">    RETVAL=$?</div><div class="line">    <span class="built_in">echo</span></div><div class="line">    <span class="built_in">return</span> <span class="variable">$RETVAL</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment"># See how we were called.</span></div><div class="line"><span class="keyword">case</span> <span class="string">"<span class="variable">$1</span>"</span> <span class="keyword">in</span></div><div class="line">    start)</div><div class="line">        start</div><div class="line">        ;;</div><div class="line">    stop)</div><div class="line">        stop</div><div class="line">        ;;</div><div class="line">    reload)</div><div class="line">        reload</div><div class="line">        ;;</div><div class="line">    restart)</div><div class="line">        stop</div><div class="line">        start</div><div class="line">        ;;</div><div class="line">    condrestart)</div><div class="line">        <span class="keyword">if</span> [ -f /var/lock/subsys/<span class="variable">$prog</span> ]; <span class="keyword">then</span></div><div class="line">            stop</div><div class="line">            start</div><div class="line">        <span class="keyword">fi</span></div><div class="line">        ;;</div><div class="line">    status)</div><div class="line">        status keepalived</div><div class="line">        RETVAL=$?</div><div class="line">        ;;</div><div class="line">    *)</div><div class="line">        <span class="built_in">echo</span> <span class="string">"Usage: <span class="variable">$0</span> &#123;start|stop|reload|restart|condrestart|status&#125;"</span></div><div class="line">        RETVAL=1</div><div class="line"><span class="keyword">esac</span></div><div class="line"></div><div class="line"><span class="built_in">exit</span> <span class="variable">$RETVAL</span></div><div class="line">&gt;&gt;&gt;&gt;&gt;&gt;&gt; .r3360</div></pre></td></tr></table></figure>
<h2 id="keepalived-的安装脚本"><a href="#keepalived-的安装脚本" class="headerlink" title="keepalived 的安装脚本"></a>keepalived 的安装脚本</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div><div class="line">144</div><div class="line">145</div><div class="line">146</div><div class="line">147</div><div class="line">148</div><div class="line">149</div><div class="line">150</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">role=<span class="variable">$1</span></div><div class="line">weight=<span class="variable">$2</span></div><div class="line">vip=<span class="variable">$3</span></div><div class="line"><span class="comment">#routerid=`ifconfig |grep Bcast|cut -d: -f2|awk '&#123;print $1&#125;'|awk -F. '&#123;print $4&#125;'`</span></div><div class="line">routerid=<span class="variable">$4</span></div><div class="line">interface=`ifconfig | awk -F<span class="string">"     "</span> <span class="string">'&#123;print $1&#125;'</span> | grep -v lo | grep -v ^\s*$`</div><div class="line"><span class="comment">#vrrd_id=`ifconfig |grep "inet addr"|grep -v "127.0.0.1"|awk -F: '&#123;print $2&#125;'|awk '&#123;print $1&#125;'|awk -F. '&#123;print $4&#125;'`</span></div><div class="line">keep_dir=/<span class="built_in">export</span>/servers/keepalived-1.2.13</div><div class="line">conf_dir=<span class="variable">$keep_dir</span>/conf</div><div class="line">pid_dir=<span class="variable">$keep_dir</span>/pid</div><div class="line">sh_dir=/<span class="built_in">export</span>/sh</div><div class="line">WHO=`whoami`</div><div class="line"><span class="keyword">function</span> <span class="built_in">help</span>()</div><div class="line">&#123;</div><div class="line">	<span class="built_in">echo</span> -e <span class="string">"Install Tips:#Such as master安装 sh <span class="variable">$0</span> MASTER 100 vip routerid # BACKUP安装 sh <span class="variable">$0</span> BACKUP [LESS THEN 100] vip routerid"</span></div><div class="line">&#125;</div><div class="line"><span class="keyword">if</span> [ <span class="variable">$#</span> -eq 0 ]</div><div class="line"><span class="keyword">then</span></div><div class="line">	<span class="built_in">help</span></div><div class="line">	<span class="built_in">exit</span> 1</div><div class="line"><span class="keyword">fi</span></div><div class="line"><span class="comment">########检查网络，安装popt包</span></div><div class="line"><span class="built_in">echo</span> -e <span class="string">"\033[;37;32mCheckNetwork\033[0m"</span></div><div class="line"><span class="keyword">if</span> ! ping -c 1 -w 1 172.22.197.62 &gt; /dev/null; <span class="keyword">then</span></div><div class="line">      <span class="built_in">echo</span> -e <span class="string">"\033[;37;31mPlease Check Network Before Setup\033[0m"</span>;</div><div class="line">      <span class="built_in">exit</span> 1;</div><div class="line"><span class="keyword">else</span> </div><div class="line">      <span class="built_in">echo</span> -e <span class="string">"\033[;37;32mOK\033[0m"</span>;</div><div class="line"><span class="keyword">fi</span></div><div class="line"><span class="built_in">echo</span> -e <span class="string">"\033[;37;32mStarting Yum\033[0m"</span></div><div class="line">yum -y install popt-devel openssl openssl-devel make openssl-devel popt popt-devel libnl-devel kernel-devel</div><div class="line"><span class="built_in">echo</span> -e <span class="string">"\033[;37;32mYum Install Done\033[0m"</span></div><div class="line"><span class="comment">####download and install keepalived-1.2.13.tar.gz</span></div><div class="line"><span class="built_in">cd</span> /usr/<span class="built_in">local</span>/src/keepalived</div><div class="line"><span class="keyword">if</span> [ ! -e keepalived-1.2.13.tar.gz ]</div><div class="line"><span class="keyword">then</span></div><div class="line">	wget http://172.22.197.62/CentOS/app/keepalived-1.2.13.tar.gz</div><div class="line">	tar xzf keepalived-1.2.13.tar.gz</div><div class="line"><span class="keyword">else</span></div><div class="line">	tar xzf keepalived-1.2.13.tar.gz</div><div class="line"><span class="keyword">fi</span></div><div class="line">rpm -ivh popt-static-1.13-7.el6.x86_64.rpm</div><div class="line"><span class="built_in">cd</span> keepalived-1.2.13</div><div class="line">./configure --prefix=/<span class="built_in">export</span>/servers/keepalived-1.2.13 --sysconfdir=/<span class="built_in">export</span>/servers/keepalived-1.2.13/etc/ --bindir=/<span class="built_in">export</span>/servers/keepalived-1.2.13/bin/ --with-kernel-dir=/usr/src/linux/</div><div class="line">make &amp;&amp; make install</div><div class="line"><span class="keyword">if</span> [ $? -eq 0 ]</div><div class="line">	<span class="keyword">then</span></div><div class="line">		<span class="built_in">echo</span> <span class="string">"make success"</span></div><div class="line">	<span class="keyword">else</span></div><div class="line">		<span class="built_in">echo</span> <span class="string">"make error"</span></div><div class="line">	  <span class="built_in">exit</span> 1</div><div class="line"><span class="keyword">fi</span></div><div class="line">cp /<span class="built_in">export</span>/servers/keepalived-1.2.13/sbin/keepalived /usr/sbin/</div><div class="line">cp /<span class="built_in">export</span>/servers/keepalived-1.2.13/etc/sysconfig/keepalived /etc/sysconfig/</div><div class="line">cp ../keepalived /etc/init.d/</div><div class="line">chmod +x /etc/init.d/keepalived</div><div class="line">chkconfig --add keepalived</div><div class="line">chkconfig keepalived on</div><div class="line"><span class="built_in">echo</span> -e <span class="string">"\033[;37;32m创建keepalive配置文件路径\033[0m"</span></div><div class="line">mkdir -p <span class="variable">$conf_dir</span></div><div class="line"><span class="comment">#######添加keepalive配置文件</span></div><div class="line">cat &gt; <span class="variable">$conf_dir</span>/keepalived.conf &lt;&lt;EOF</div><div class="line">! Configuration File <span class="keyword">for</span> keepalived</div><div class="line"></div><div class="line">global_defs &#123;</div><div class="line">   notification_email &#123;</div><div class="line">      lihuiyw@jd.com   </div><div class="line">&#125;</div><div class="line">   notification_email_from lihuiyw@jd.com</div><div class="line">   smtp_server mail.jd.com</div><div class="line">   smtp_connect_timeout 30</div><div class="line">   router_id LVS_DEVEL</div><div class="line">&#125;</div><div class="line">vrrp_script check_alive</div><div class="line">&#123;</div><div class="line">        script <span class="string">"/export/sh/check_nginx_alive.sh"</span></div><div class="line">        <span class="comment"># check every 2 seconds</span></div><div class="line">        interval 2</div><div class="line">        <span class="comment"># if failed, decrease 10 of the priority</span></div><div class="line">        weight -10</div><div class="line">        <span class="comment"># require 2 failures for failures</span></div><div class="line">        fail 2</div><div class="line">        <span class="comment"># require 1 sucesses for ok</span></div><div class="line">        rise 1</div><div class="line">&#125;</div><div class="line">vrrp_instance VIP_<span class="variable">$&#123;routerid&#125;</span></div><div class="line">&#123;</div><div class="line">	state <span class="variable">$role</span></div><div class="line">	interface <span class="variable">$interface</span></div><div class="line">	virtual_router_id <span class="variable">$routerid</span></div><div class="line">	priority <span class="variable">$weight</span></div><div class="line">	advert_int 2 </div><div class="line">	garp_master_delay 10</div><div class="line">	smtp_alert</div><div class="line"></div><div class="line">	authentication</div><div class="line">	&#123;</div><div class="line">		auth_type PASS </div><div class="line">		auth_pass 123456</div><div class="line">	&#125;</div><div class="line">	virtual_ipaddress</div><div class="line">	&#123; </div><div class="line">		<span class="variable">$&#123;vip&#125;</span>/24</div><div class="line">	&#125;</div><div class="line">	track_interface </div><div class="line">	&#123;</div><div class="line">		<span class="variable">$interface</span></div><div class="line">	&#125; </div><div class="line">	track_script</div><div class="line">	&#123; </div><div class="line">		check_alive </div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line">EOF</div><div class="line"><span class="comment">###########监控keepalived脚本、脚本需要放到同keepalived配置文件指定路径</span></div><div class="line"><span class="built_in">echo</span> -e <span class="string">"\033[;37;32m创建脚本路径\033[0m"</span></div><div class="line">mkdir -p <span class="variable">$sh_dir</span> <span class="variable">$pid_dir</span></div><div class="line">cat &gt; <span class="variable">$sh_dir</span>/check_nginx_alive.sh &lt;&lt; EOF</div><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="comment">#监控脚本功能概述：</span></div><div class="line"><span class="comment">#首先检查进程中的nginx进程数目，如果不存在（即为0）,则表示nginx未开启，然后开启nginx。</span></div><div class="line"><span class="comment">#3秒后重新检查nginx进程数，若仍为0，则表示nginx无法正常启动，此时强制停止keepalived进程，让虚拟ip切换到backup服务器上</span></div><div class="line"></div><div class="line"><span class="comment">## 如果没有nginx进程，即值为零</span></div><div class="line"><span class="comment">#echo \`pgrep -l nginx\`</span></div><div class="line"><span class="comment">#echo \`ps -ef | grep nginx\`</span></div><div class="line"></div><div class="line"><span class="keyword">if</span> [ \`pgrep <span class="string">"^nginx"</span> | wc -l\` -eq 0 ]</div><div class="line"><span class="keyword">then</span></div><div class="line">	service nginx start	</div><div class="line"></div><div class="line">	sleep 1</div><div class="line"></div><div class="line">	<span class="keyword">if</span> [ \`pgrep <span class="string">"^nginx"</span> | wc -l\` -eq 0 ]</div><div class="line">	<span class="keyword">then</span></div><div class="line">		<span class="comment">## 则结束 keepalived 进程，使得服务器切换到BACKUP服务器上</span></div><div class="line">		service keepalived stop</div><div class="line">	<span class="keyword">fi</span></div><div class="line"><span class="keyword">fi</span></div><div class="line">EOF</div><div class="line"><span class="built_in">echo</span> -e <span class="string">"\033[;37;32m赋可执行权限\033[0m"</span></div><div class="line">chown -R admin.admin <span class="variable">$conf_dir</span> <span class="variable">$keep_dir</span> <span class="variable">$sh_dir</span></div><div class="line"><span class="comment">#chmod 777 $sh_dir/check_nginx_alive.sh</span></div><div class="line">/<span class="built_in">export</span>/servers/keepalived-1.2.13/sbin/keepalived --use-file=/<span class="built_in">export</span>/servers/keepalived-1.2.13/conf/keepalived.conf</div><div class="line">&gt;/home/install_keepalived.log</div><div class="line"><span class="keyword">if</span> [ $? -eq 0 ]</div><div class="line">	<span class="keyword">then</span></div><div class="line">		<span class="built_in">echo</span> <span class="string">"keepalived install done"</span></div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/LVS/" rel="tag"># LVS</a>
          
            <a href="/tags/keepalived/" rel="tag"># keepalived</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/09/03/LVS/4. keepalived 单独配置文件/" rel="next" title="keepalived 单独配置文件">
                <i class="fa fa-chevron-left"></i> keepalived 单独配置文件
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/09/03/LVS/7. keepalived 和 lvs 的结合配置文件详解/" rel="prev" title="keepalived 和 lvs 的结合配置文件详解">
                keepalived 和 lvs 的结合配置文件详解 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/0.png"
               alt="失落的乐章" />
          <p class="site-author-name" itemprop="name">失落的乐章</p>
           
              <p class="site-description motion-element" itemprop="description">失落的乐章的Blog</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/">
                <span class="site-state-item-count">627</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">38</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/hcldirgit" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#keepalived-的启动脚本"><span class="nav-number">1.</span> <span class="nav-text">keepalived 的启动脚本</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#keepalived-的安装脚本"><span class="nav-number">2.</span> <span class="nav-text">keepalived 的安装脚本</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">失落的乐章</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.1"></script>



  


  




	





  





  





  






  





  

  

  

  

  

  

</body>
</html>
